package com.zhrenjie04.alex.manager.controller;

import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.netflix.hystrix.exception.HystrixRuntimeException;
import com.zhrenjie04.alex.core.exception.CrisisError;
import com.zhrenjie04.alex.core.exception.UnauthorizedException;

/**
 * @author 张人杰
 */
@ControllerAdvice
public class ExceptionControllerAdvice {

	private static final Logger logger = Logger.getLogger(ExceptionControllerAdvice.class);

	@ExceptionHandler(value = UnauthorizedException.class)
	public ResponseEntity<String> unauthorizedException(Exception exception) {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		return new ResponseEntity<String>(
				"{\"status\":401,\"message\":\""
						+ exception.getMessage().replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\r\\\\n") + "\"}",
				headers, HttpStatus.UNAUTHORIZED);
	}

	@ExceptionHandler(value = CrisisError.class)
	public ResponseEntity<String> crisisError(CrisisError error) {
		logger.error("", error);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		return new ResponseEntity<String>(
				"{\"status\":500,\"message\":\""
						+ error.getMessage().replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\r\\\\n") + "\"}",
				headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	@ExceptionHandler(value = HystrixRuntimeException.class)
	public ResponseEntity<String> hystrixRuntimeException(HystrixRuntimeException exception) {
		logger.error("", exception);
		logger.error("fallback:", exception.getFallbackException());
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		return new ResponseEntity<String>(
				"{\"status\":500,\"message\":\"" + exception.getFallbackException().getCause().getCause().getMessage()
						.replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\r\\\\n") + "\"}",
				headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	@ExceptionHandler(value = Exception.class)
	public ResponseEntity<String> exception(Exception exception) {
		logger.error("", exception);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		return new ResponseEntity<String>(
				"{\"status\":500,\"message\":\""
						+ exception.getMessage().replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\r\\\\n") + "\"}",
				headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}

	@ExceptionHandler(value = Error.class)
	public ResponseEntity<String> error(Error error) {
		logger.error(error);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		return new ResponseEntity<String>(
				"{\"status\":500,\"message\":\""
						+ error.getMessage().replaceAll("(\\r\\n|\\r|\\n|\\n\\r)", "\\\\r\\\\n") + "\"}",
				headers, HttpStatus.INTERNAL_SERVER_ERROR);
	}
}
